<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the functions file
    require_once('functions.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
    include_once('classes/geshi/geshi.php');  //Geshi file


    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['displayfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Viewing file'));
    } else {
        //Connect to to the database using values provided by db.php
        $db = new my_db;
        $id = strip_tags(mysql_escape_string($_GET['id']));
        $rev = strip_tags(mysql_escape_string($_GET['rev']));
         
        $db->query("SELECT * FROM trackedfiles WHERE id='$id'");
        $db->next_record();
        if ($db->nf() > 0) {
            $latestrev = strip_tags($db->f('latestrev'));
            $filename = strip_tags($db->f('filename'));
            $filepath = strip_tags($db->f('filepath'));
            $filetype = strip_tags($db->f('filetype'));
            $latestrev = mysql_escape_string($db->f('latestrev'));
            $db->query("SELECT * FROM history WHERE revhash='$rev'");
            $db->next_record();
            $user = strip_tags($db->f('user'));
            $date = strip_tags($db->f('revdate'));
            
            $mime = explode("/", $filetype);
            $filearray = explode('.', $filename);
            if (count($filearray) > 1) {
                $ep = count($filearray) -1;
                $extension = $filearray[$ep];
            }
            template_headtag("Viewing $filename");
            template_header();
            template_left();
            echo "<div class='pageheadertext'>Viewing $filename in</div>";
            echo "<div class='subheadertext'>$filepath</div><br/>";

            if (!empty($rev)) {
                echo "<div class='subheadertext2'>History version of $filename updated by $user on $date</div><br />";
                echo "<!--<p>Revhash: $rev</p><br />-->";
                $latestrev = $rev;
            }
             
            if ($mime[0] == "text") {
                $db->query("SELECT * FROM content WHERE dataid = '$latestrev'");
                if ($db->nf() > 0) {
                    $db->next_record();
                    $content = $db->f('content');

                    echo "<div class='basicbox'>";
                    
                    $geshi = new GeSHi($content, $extension);
                
                    $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS,5);
                    $geshi->set_line_style('background: #fff;', 'background: #efefef;', true);
                    echo $geshi->parse_code();

                    echo "</div><br/>";
                     
                } else {
                    errorbox('Content not found!');
                }
            } else {
                 
                if ($mime[0] == "image") {
                    echo "<div align='center'>Image:<br/>";
                    echo "<img src='displaybin.php?id=$latestrev' border='0' /></div>";
                } else {
                    echo "<script>document.location.href='displaybin.php?id=$latestrev'</script>";
                }
            }
            echo "<br/>";
             
            echo "<a href='javascript: history.go(-1)'>Back</a>";
            echo "<br/>";
        } else {
            errorpage('File not found. Invaild ID!', 'Viewing file');
        }
        template_footer();
         
         
    }
?>
